package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.Paper;
import com.leoman.service.*;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 一言管理
 * Created by 涂奕恒 on 2016/12/6.
 */
@Controller
@RequestMapping(value = "backend/paper")
public class PaperController extends CommonController {

    @Autowired
    private PaperService paperService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private PraiseService praiseService;

    @Autowired
    private PaperImageService paperImageService;

    @RequestMapping("index")
    public String index() {
        return "backend/一言列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String userName,
                     String startTime,
                     String endTime,
                     Integer type,
                     String num,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Paper> page = paperService.pageByParams(userName, startTime, endTime, type, num, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("add")
    public String add(ModelMap model, Integer paperId) {
        if (null != paperId) {
            Paper paper = paperService.getById(paperId);
            paper.setCommentNum(commentService.findListBySourceIdAndType(paperId, Constant.SOURCE_TYPE_PAPER).size());
            paper.setPraiseNum(praiseService.findListBySourceIdAndType(paperId, Constant.SOURCE_TYPE_PAPER).size());
            model.addAttribute("paper", paper);
            model.addAttribute("createTime", DateUtils.longToStringWithFormat(paper.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
            model.addAttribute("paperImage", paperImageService.getById(paper.getPaperId()));

            return "backend/一言详情";
        } else {
            return "backend/添加一言";
        }
    }

    /**
     * 置顶一言
     *
     * @param paperId 一言ID
     */
    @RequestMapping("/toTop")
    @ResponseBody
    public Integer toTop(Integer paperId) {
        try {
            return paperService.toTop(paperId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加or修改一言
     */
    @RequestMapping("/save")
    @ResponseBody
    public Integer save(String content, Integer typeId, Integer paperId) {
        try {
            return paperService.savePaper(content, typeId, paperId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除一言
     *
     * @param paperId 一言ID
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer paperId) {
        try {
            paperService.deleteById(paperId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除一言
     *
     * @param ids 一言ID
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            return paperService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
